<template>
  <div class="login">
    <div class="wraper">
      <div class="header">Login</div>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="username" prop="username" class="user">
          <el-input
            type="text"
            v-model="ruleForm.username"
            autocomplete="off"
            class="user-input"
          ></el-input>
        </el-form-item>
        <el-form-item label="password" prop="password" class="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="primary"  @click="submitForm('ruleForm')"
            >Login</el-button
          >
          <el-button  @click="resetForm('ruleForm')">Reset</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import jwt from "jwt-decode";
import { mapMutations } from "vuex";
export default {
  data() {
    var validateuser = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入账号"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [{ validator: validateuser, trigger: "blur" }],
        password: [{ validator: validatePass, trigger: "blur" }],
      },
    };
  },
  methods: {
    ...mapMutations("LoginModule", ["setUser"]),
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let { username, password } = this.ruleForm;
          this.$api
            .getLogin({
              username,
              password,
            })
            .then((res) => {
              console.log(res.data.data);
              if (res.data.status === 200) {
                //    this.$router.push('/')
                let obj = {
                  user: jwt(res.data.data).username,
                  token: res.data.data,
                };
                this.setUser(obj);
                localStorage.setItem("user", JSON.stringify(obj));

                this.$router.push("/");
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style lang="less" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 844px;
  background-image: linear-gradient(to right, #fbc2eb, #a6c1ee);
  .wraper {
    background-color: #fff;
    width: 350px;
    height: 450px;
    border-radius: 15px;
    .demo-ruleForm {
      position: relative;
      top: 0;
      left: -10%;
      .user {
       padding-left: 20px;
       .el-form-item__label{
           color: #a6c1ee;
       }
      }
      .password {
        padding-left: 20px;
      }
    }
  }
  .header {
    font-size: 30px;
    font-weight: bold;
    text-align: center;
    line-height: 200px;
  }
}
.btn {
    margin-top: 51px;
    margin-left: 39px;
  .el-button{
       background-image: linear-gradient(to right,#fbc2eb,#a6c1ee);;
       border: none;
       color: #fff;
  }
}
/deep/.el-input__inner{
    color: #a6c1ee;
    font-size: 16px;
}
</style>